using System;
using Microsoft.SPOT;
using netduino.helpers.Imaging;

namespace PeteBrown.Sixty4Racer
{
    class Screens
    {
        // Font: ScreensSource.pf
        private const int ScreenCount = 32;
        private const int ScreenHeight = 8;
        private readonly byte[] _screenBytes = new byte[ScreenCount * ScreenHeight]
        {
	        0xC7, 0xC3, 0x83, 0x83, 0x81, 0x00, 0x00, 0x00,	// Start Screen
	        0x00, 0x00, 0x81, 0x81, 0xC3, 0xC3, 0xE7, 0xE7,	// Stop Screen
	        0xE3, 0xCF, 0x87, 0xCF, 0x81, 0xF3, 0xC3, 0x87,	// Char 002 (.)
	        0x87, 0xC1, 0xF1, 0xF9, 0xE1, 0x87, 0xC3, 0x81,	// Char 003 (.)
	        0x81, 0xE7, 0x81, 0x99, 0x99, 0x81, 0xE7, 0xC3,	// Char 004 (.)
	        0xE7, 0xE7, 0xF1, 0xC1, 0xCF, 0xE3, 0xF7, 0xE7,	// Char 005 (.)
	        0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7,	// Char 006 (.)
	        0xE3, 0xF9, 0xFC, 0xFE, 0xFC, 0xF9, 0xF3, 0xE7,	// Char 007 (.)
	        0xE7, 0xE7, 0xEF, 0xE7, 0xF7, 0xE7, 0xEF, 0xE7,	// Char 008 (.)
	        0xC3, 0xFB, 0xE1, 0xEF, 0xC1, 0xFB, 0x83, 0xE7,	// Char 009 (.)
	        0xE7, 0xC1, 0x81, 0xF9, 0xC0, 0x81, 0x9F, 0x87,	// Char 010 (.)
	        0xE7, 0xC3, 0x00, 0x7E, 0x00, 0xE7, 0xE7, 0x81,	// Char 011 (.)
	        0xE7, 0x81, 0x18, 0x7E, 0x3C, 0x99, 0xC3, 0xE7,	// Char 012 (.)
	        0x00, 0x18, 0x3C, 0x3C, 0x3C, 0x3C, 0x18, 0x00,	// Char 013 (.)
	        0xC3, 0x89, 0x24, 0xB5, 0xA5, 0x2C, 0x81, 0xC3,	// Char 014 (.)
	        0x86, 0x3C, 0x5A, 0x66, 0x3C, 0x28, 0x82, 0xC6,	// Char 015 (.)
	        0x81, 0x00, 0x66, 0x66, 0x00, 0x99, 0x81, 0xA5,	// Char 016 (.)
	        0xE7, 0xC3, 0xDF, 0x83, 0xF7, 0xA1, 0x8D, 0xE7,	// Char 017 (.)
	        0xC3, 0x81, 0x00, 0x18, 0x18, 0x00, 0x81, 0xC3,	// Char 018 (.)
	        0xE7, 0xC3, 0x99, 0x38, 0x1C, 0x99, 0xC3, 0xE7,	// Char 019 (.)
	        0x04, 0x49, 0x02, 0x10, 0x4A, 0x00, 0x44, 0x20,	// Char 020 (.)
	        0xA7, 0x2D, 0x6A, 0x2D, 0x26, 0x55, 0x56, 0x27,	// Char 021 (.)
	        0xE7, 0xF7, 0xE7, 0xF7, 0xC3, 0xDB, 0xC3, 0xE7,	// Char 022 (.)
	        0xE3, 0xCB, 0x9B, 0x3B, 0x89, 0x3D, 0x9D, 0xC5,	// Char 023 (.)
	        0xE7, 0xC3, 0x99, 0xBD, 0x99, 0xC3, 0xE7, 0xE7,	// Char 024 (.)
	        0xC3, 0xCF, 0xC3, 0xF3, 0xC7, 0xE3, 0xCF, 0xC7,	// Char 025 (.)
	        0x65, 0xB6, 0x55, 0xB6, 0x65, 0xAA, 0x6D, 0xA7,	// Char 026 (.)
	        0xE7, 0xC3, 0xE3, 0xF1, 0xF8, 0xF3, 0xC7, 0xE7,	// Char 027 (.)
	        0xE4, 0xF4, 0xF4, 0xF4, 0xE4, 0x69, 0x0B, 0xA7,	// Char 028 (.)
	        0x00, 0x18, 0x66, 0x5A, 0x5A, 0x66, 0x18, 0x00,	// Char 029 (.)
	        0xA5, 0x91, 0x93, 0x97, 0x93, 0xCB, 0xE3, 0xE7,	// Char 030 (.)
	        0x00, 0xDB, 0xC3, 0xE7, 0xE7, 0xC3, 0x99, 0x00	// Char 031 (.)
        };

        private RacerGame _parentGame;

        public Screens(RacerGame parentGame)
        {
            _parentGame = parentGame;
        }


        public int GetLevelScreenCount(int level)
        {
            return level * 6 + 2;
        }

        public int GetLevelPixelHeight(int level)
        {
            return GetLevelScreenCount(level) * ScreenHeight;
        }

        public Composition GetLevelComposition(int level)
        {
            int levelScreenCount = GetLevelScreenCount(level);

            byte[] bytes = new byte[ScreenHeight * levelScreenCount];

            int b = 0;
            int startIndex = 0;

            for (int i = 0; i < levelScreenCount; i++)
            {
                if (i == 0)
                {
                    // finish screen. This is the second screen in the array
                    startIndex = ScreenHeight;
                }
                else if (i == levelScreenCount - 1)
                {
                    // start screen. First screen in the array
                    startIndex = 0;
                }
                else
                {
                    // regular random screen
                    int selectedScreen = _parentGame.Random.Next(ScreenCount - 2) + 2;

                    startIndex = ScreenHeight * selectedScreen;
                }

                for (int a = startIndex; a < startIndex + ScreenHeight; a++, b++)
                {
                    bytes[b] = _screenBytes[a];
                }
            }

            return new Composition(bytes, 8, levelScreenCount * ScreenHeight);
        }

    }
}
